package com.hua.api.modules.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.framework.util.exception.ApiError;
import com.hua.api.core.util.StringUtil;
import com.hua.api.modules.order.domain.OrderInfoLogger;
import com.hua.api.modules.order.repository.OrderInfoLoggerRepository;
import com.hua.api.modules.order.service.OrderInfoLoggerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderInfoLoggerServiceImpl implements OrderInfoLoggerService {

    private final OrderInfoLoggerRepository orderInfoLoggerRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrderInfoLogger(JSONObject data) {
        log.info("OrderInfoLoggerServiceImpl.saveOrderInfoLogger data={}",data.toJSONString());
        OrderInfoLogger ol = new OrderInfoLogger();
        Integer memberId = data.getInteger("memberId");
        Integer userId = data.getInteger("userId");
        Integer orderId = data.getInteger("orderId");
        String operateName = data.getString("operateName");
        String operateDesc = data.getString("operateDesc");
        String operateEvent = data.getString("operateEvent");

        if(StringUtil.isEmptys(operateName,operateDesc,orderId,operateEvent)){
            throw ApiError.Type.LOGIC_ERROR.toException("缺少必要参数！");
        }
        ol.setCreatedTime(new Date());
        ol.setMemberId(memberId);
        ol.setUserId(userId);
        ol.setOrderId(orderId);
        ol.setOperateName(operateName);
        ol.setOperateDesc(operateDesc);
        ol.setOrderEvent(operateEvent);

        orderInfoLoggerRepository.save(ol);

    }
}